/**
 * 
 */
package specialtriggers;

import java.io.FileWriter;
import java.io.PrintWriter;

import account.Account;
import bot.IRCBot;
import trigger.VariableTrigger;

/**
 * @author Philip
 *
 */
public class NewAccountTrigger extends VariableTrigger {
	private IRCBot ircBot;
	private String input;
	private String senderHostname;

	public NewAccountTrigger(String trigger, String response, IRCBot ircBot) {
		super(trigger, response, 2);
		this.ircBot = ircBot;
	}

	public String response(int i) {
		String accountText = input.substring(input.indexOf("register: ") + 10);
		String[] segments = accountText.split(" ");
		try {
			ircBot.addAccount(senderHostname, new Account(segments[0], segments[1], 2));

			PrintWriter outputWriter = new PrintWriter(new FileWriter("accounts.txt", true));
	        outputWriter.println(accountText + " 2 " + senderHostname);
	        outputWriter.close();
		} catch (Exception e) {
			return "Inkorrekt syntax!!!! :<<< (" + e.getMessage() + ")";
		}
		return super.response(i);
	}
	
	public void updateVariables(String nickName, String sender, String receiver, String senderHostname, String input) {
		this.input = input;
		this.senderHostname = senderHostname;
		super.updateVariables(nickName, sender, receiver, senderHostname, input);
	}
}
